import 'package:flutter/material.dart';

import 'package:flutter/material.dart';
import 'HomeWidget.dart';
import 'HomeWidget1.dart';
import 'ZhongcaoMao.dart';
import 'HuodongZhongxin.dart';
import 'ShoppingCart.dart';
import 'Member.dart';
import 'Member1.dart';
//import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter/services.dart';
import 'package:flutter/services.dart';
import 'dart:io';
import 'package:flutter_app/utils/windowUtil.dart';
import 'Conversation.dart';
import 'Member2.dart';
import 'YunPinCang.dart';
import 'Rank.dart';

class MainWidget1 extends StatefulWidget {
  @override
  State<MainWidget1> createState() => MainWidget1State();
}

class MainWidget1State extends State<MainWidget1>
    with SingleTickerProviderStateMixin {
  int currentPage = 0;
  var _pageController = new PageController(initialPage: 0);
  int count = 0;
  YunPinCang yunPinCang = YunPinCang();
  Conversation conversation = Conversation();
  Rank rank = Rank();
  ShoppingCart shoppingCart;
  Member2 member2 = Member2();
  @override
  void initState() {
    super.initState();
    shoppingCart = ShoppingCart((val) => onDataChange(val));
  }

  @override
  bool get wantKeepAlive => true;
  @override
  Widget build(BuildContext context) {
    print("building>");
//Fluttertoast.showToast(msg: "building");
    List<Widget> list = List();
    // list.add(HomeWidget1());
    list.add(yunPinCang);
    // list.add(ZhongcaoMao());
    // list.add(conversation);
    //  list.add(HuodongZhongxin());
    list.add(rank);
    list.add(shoppingCart);
    list.add(member2);
    return Container(
      height: windowUtil.height,
      child: WillPopScope(
          child: SafeArea(
              top: false,
              child: Scaffold(
                body: PageView.builder(
                  controller: _pageController,
                  physics: NeverScrollableScrollPhysics(),
                  itemBuilder: (c, int b) {
                    if (currentPage == 0) {
                      return yunPinCang;
                    }
//              if(currentPage==1){
//                return conversation;
//              }
                    if (currentPage == 1) {
                      return rank;
                    }
                    if (currentPage == 2) {
                      return shoppingCart;
                    }
                    if (currentPage == 3) {
                      return member2;
                    }
                  },
                ),
                bottomNavigationBar: BottomNavigationBar(
                  elevation: 0.0,
                  items: [
                    BottomNavigationBarItem(
                        icon: Container(
                          width: 30.0,
                          height: 30.0,
                          child: Image.asset("images/homenot.png"),
                        ),
                        activeIcon: Container(
                          width: 30.0,
                          height: 30.0,
                          child: Image.asset("images/homeselect.png"),
                        ),
                        title: Text(
                          "首页",
                          style: TextStyle(
                              color: currentPage == 0
                                  ? Color(0xffd81e06)
                                  : Color(0xffeeeeee)),
                        )),
//              BottomNavigationBarItem(icon: Image.asset("images/haicao.png"),
//                  activeIcon: Image.asset("images/haicaoselect.png"),
//                  title:Text("种草猫",style: TextStyle(color: getTextColor(1)),) ),
                    BottomNavigationBarItem(
                        icon: Container(
                          width: 30.0,
                          height: 30.0,
                          child: Image.asset("images/huodong.png"),
                        ),
                        activeIcon: Container(
                          width: 30.0,
                          height: 30.0,
                          child: Image.asset("images/huodongselect.png"),
                        ),
                        title: Text(
                          "分类搜索",
                          style: TextStyle(color: getTextColor(1)),
                        )),
                    BottomNavigationBarItem(
                        icon: bage(),
                        activeIcon: bage1(),
                        title: Text(
                          "购物车",
                          style: TextStyle(color: getTextColor(2)),
                        )),
                    BottomNavigationBarItem(
                        icon: Container(
                          child: Image.asset("images/people.png"),
                          width: 30.0,
                          height: 30.0,
                        ),
                        activeIcon: Container(
                          width: 30.0,
                          height: 30.0,
                          child: Image.asset("images/peopleselect.png"),
                        ),
                        title: Text(
                          "我的",
                          style: TextStyle(color: getTextColor(3)),
                        )),
                  ],
                  type: BottomNavigationBarType.fixed,
                  currentIndex: currentPage,
                  onTap: (i) {
                    setState(() {
                      currentPage = i;
                    });
                  },
                ),
                bottomSheet: Container(
                  color: Colors.grey,
                  height: 1.0,
                ),
              )),
          onWillPop: () {
            SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          }),
    );
  }

  Color getTextColor(int index) {
    if (currentPage == index) {
      return Color(0xffd81e06);
    } else {
      return Color(0xffeeeeee);
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  onDataChange(int val) {
    setState(() {
      count = val;
    });

//    Fluttertoast.showToast(msg: "update");
  }

  Widget bage() {
    return Stack(
      alignment: Alignment.topRight,
      children: <Widget>[
        Container(width: 30.0,height: 30.0,child:Image.asset("images/gouwuche.png") ,)
        ,
        Opacity(
          opacity: count == 0 ? 0.0 : 1.0,
          child: Container(
            width: 15.0,
            height: 15.0,
            child: Center(
              child: Text(
                "$count",
                style: TextStyle(color: Colors.white, fontSize: 10.0),
              ),
            ),
            decoration:
                BoxDecoration(color: Colors.red, shape: BoxShape.circle),
          ),
        )
      ],
    );
  }

  Widget bage1() {
    return Stack(
      alignment: Alignment.topRight,
      children: <Widget>[
        Container(width: 30.0,height: 30.0,child: Image.asset("images/gouwucheselect.png"),)
        ,
        Opacity(
          opacity: count == 0 ? 0.0 : 1.0,
          child: Container(
            width: 15.0,
            height: 15.0,
            child: Center(
              child: Text(
                "$count",
                style: TextStyle(color: Colors.white, fontSize: 10.0),
              ),
            ),
            decoration:
                BoxDecoration(color: Colors.red, shape: BoxShape.circle),
          ),
        )
      ],
    );
  }
}
